﻿$(function () {
    //点击链接弹出窗口
    $("input[type='button'].addUpdateMember").live("click", function () {
        cleanForm();
        $("div.popupWindow").show("slow");
        $('#iptStartDate').datepicker();
        var btnCurrent = this;
        $.ajax({
            url: "../../member/getAllTeamAndPositonAndRoles",
            type: "get",
            dataType: "json",
            success: function (data) {
                $("#selTeam").empty();
                $("#selPosition").empty();
                $("#selRoles").empty();
                var $parent = $(btnCurrent).parent().parent();
                $.each(data.team, function (i, item) {
                    if (btnCurrent.name == "update") {
                        var teamAlias = $.trim($("td:eq(2)", $parent).text());
                        if (teamAlias == item.teamAlias) {
                            $("#selTeam").append("<option value='" + item.ID + "' selected='selected'>" + item.teamAlias + "</option>");
                        } else {
                            $("#selTeam").append("<option value='" + item.ID + "' >" + item.teamAlias + "</option>");
                        }
                    }
                    else {
                        $("#selTeam").append("<option value='" + item.ID + "' selected='selected'>" + item.teamAlias + "</option>");
                    }
                });
                $.each(data.position, function (i, item2) {
                    if (btnCurrent.name == "update") {
                        var posAlias = $.trim($("td:eq(3)", $parent).text());
                        if (posAlias == item2.positionAlias) {
                            $("#selPosition").append("<option value='" + item2.ID + "' selected='selected'>" + item2.positionAlias + "</option>");
                        } else {
                            $("#selPosition").append("<option value='" + item2.ID + "'>" + item2.positionAlias + "</option>");
                        }
                    } else {
                        $("#selPosition").append("<option value='" + item2.ID + "'>" + item2.positionAlias + "</option>");
                    }
                });
                $.each(data.roles, function (i, item3) {
                    if (btnCurrent.name == "update") {
                        var userRole = $.trim($("td:eq(6)", $parent).text());
                        if (userRole == item3.Roles) {
                            $("#selRoles").append("<option value='" + item3.Roles + "' selected='selected'>" + item3.Roles + "</option>");
                        } else {
                            $("#selRoles").append("<option value='" + item3.Roles + "'>" + item3.Roles + "</option>");
                        }
                    } else {
                        $("#selRoles").append("<option value='" + item3.Roles + "'>" + item3.Roles + "</option>");
                    }
                });
                $("#selTeam").append("<option value='0' >" + "其他" + "</option>");
                $("#selPosition").append("<option value='0' >" + "其他" + "</option>");
            }
        });
        if (this.name == "update") {
            $("h2:eq(0)", $("div.popupWindow")).attr("id", "updateMemberTitle").html("更新员工信息");
            $("input#alias").attr("readonly", true);
            var $parent = $(this).parent().parent();
            $("input#alias").val($.trim($("td:eq(1)", $parent).text()));
            $("input#email").val($.trim($("td:eq(4)", $parent).text()));
            $("input#iptStartDate").val($.trim($("td:eq(5)", $parent).text()));
        } else {
            $("h2:eq(0)", $("div.popupWindow")).attr("id", "newAddMemberTitle").html("添加新员工信息");
            $("input#alias").removeAttr("readonly");
            $("input#alias").val("")
        }
        $("#alias").focus();
        //获取页面文档的高度
        var docheight = $(document).height();
        //追加一个层，使背景变灰
        $("#greybackground").css({ "opacity": "0.5", "height": docheight });
        $("#greybackground").show();
        return false;
    });
    //select change event
    $("select#selTeam").change(function () {
        if (this.value == 0) {
            $("input#iptTeam").show();
        } else {
            $("input#iptTeam").hide();
        }
    });

    $("select#selPosition").change(function () {
        if (this.value == 0) {
            $("input#iptPosition").show();
        } else {
            $("input#iptPosition").hide();
        }
    });
    //提交表单
    $("input#btnSubmit").click(function () {
        if ($("input#alias").val() == "") {
            $("span", $("input#alias").parent()).text("alias工号不能为空！");
            return false;
        }
        var teamValue = $("select#selTeam option:selected").val();
        if (teamValue == '0') teamValue = $.trim($("#iptTeam").val());
        if (teamValue == '0') {
            $("span", $("select#selTeam").parent()).text("所属部门不能为空！");
            return false;
        }
        var positionValue = $("select#selPosition").val();
        if (positionValue == 0) positionValue = $.trim($("#iptPosition").val());
        if (positionValue == 0) {
            $("span", $("select#selPosition").parent()).text("职称不能为空！");
            return false;
        }
        var userAlias = $.trim($("input#alias").val());
        var emailValue = $("#email").val();
        var emailReg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9\-]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        if (!emailReg.test(emailValue)) {
            $("span", $("#email").parent()).text("邮箱格式不正确！");
            return false;
        }
        var requestUrl = "../../member/submit";
        if ($("h2:eq(0)", $("div.popupWindow")).attr("id") == "updateMemberTitle") {
            requestUrl = "../../member/update";
        }
        $.ajax({
            url: requestUrl,
            type: "POST",
            data: { "UserName": userAlias, "Role": $("select#selRoles option:selected").val(), "Team": teamValue, "Position": positionValue, "email": emailValue, "startDate": $("#iptStartDate").val() },
            dataType: "json",
            success: function (data) {
                switch (data.status) {
                    case 1:
                        $("span", $("input#alias").parent()).text("员工编号已经存在！");
                        break;
                    case 2:
                        if (requestUrl == "../../member/update") {
                            $("div.popupWindow").hide();
                            $("#greybackground").hide();
                            alert("更新成功！");
                            window.location.reload();
                        } else {
                            loadAssetsInfo(userAlias, true);
                        }
                        break;
                    case 4:
                    default:
                        alert("出错啦，联系管理员！");
                        break;
                }
            }
        });
    });
    function loadAssetsInfo(userAlias, isFirstload) {
        if (isFirstload) {
            $("div.popupWindow").empty();
            $("div.popupWindow").append($("div.hiddenAsset").children());
            $("div.popupWindow").append(("<input type='hidden' id='hideNewUserAlias' value='" + userAlias + "' />"));
            $("h2:eq(0)", $("div.popupWindow")).html("为用户<b>" + userAlias + "</b>添加资产");
        }
        $("#popupwindow_asset").find('tr').not(':first,:last').remove();
        $.ajax({
            url: "../../asset/getAllAssetInfo",
            type: "GET",
            data: { "alias": userAlias },
            dataType: "json",
            success: function (data) {
                if (data.status == 2) {
                    $("#selAssetID").empty();
                    $.each(data.assets, function (i, item) {
                        $("#selAssetID").append("<option value='" + item.ID + "'>" + item.ID + "</option>");
                    });
                    $("#selAssetID").prepend("<option value='0'>请选择...</option>");
                }
            }
        });
    }
    //asset change event
    $("select#selAssetID").live("change", function () {
        $("#popupwindow_asset").find('tr').not(':first,:last').remove();
        var assetID = this.value;
        if (this.value != 0) {
            $.ajax({
                url: "../../asset/getAllAssetInfoByAssetID",
                type: "GET",
                data: { "assetID": assetID },
                dataType: "json",
                success: function (data) {
                    if (data.status == 2) {
                        if (data.MAC != "暂无MAC") {
                            var appendTr = "<tr><td>Owner:</td><td>" + data.owner + "</td><td>MAC:</td><td>" + data.MAC + "</td></tr><tr><td>网络状态:</td><td>" + data.nstatus + "</td><td>最近更新日期:</td><td>" + data.date + "</td></tr><tr><td>资产ID:</td><td>" + data.assetID + "</td><td>型号：</td><td>" + data.model + "</td></tr><tr><td>资产类型:</td><td>" + data.type + "</td><td>S/N：</td><td>" + data.Sn + "</td></tr><tr><td>描述：</td><td>" + data.description + "</td></tr>";
                        }
                        else {
                            var appendTr = "<tr><td>Owner:</td><td>" + data.owner + "</td><td>资产ID:</td><td>" + data.assetID + "</td></tr><tr><td>型号:</td><td>" + data.model + "</td><td>资产类型:</td><td>" + data.type + "</td></tr><tr><td>S/N：</td><td>" + data.Sn + "</td><td>描述：</td><td>" + data.description + "</td></tr>";
                        }
                        $("#popupwindow_asset").find('tr:last').before(appendTr);
                    }
                }
            });
        }
    });
    //清空表单
    $("input#btnReset").click(function () {
        cleanForm();
    });
    //删除选中资产
    $("input#btnDelete").click(function () {
        if ($("input[type='checkbox']:checked", $("#tblMain tr td")).length == 0) {
            alert("请选择需要删除的Alias");
            return;
        }
        if (confirm("确定删除选中的" + $("input[type='checkbox']:checked", $("#tblMain tr td")).length + "项alias?")) {
            var queryData = "";
            var ids = $("input[type='checkbox']:checked", $("#tblMain tr td"));
            $.each(ids, function (i, item) {
                if (i == 0) {
                    queryData = item.id;
                } else {
                    queryData += '||' + item.id;
                }
            });
            $.ajax({
                url: "../../member/delete",
                type: "POST",
                data: { "ids": queryData },
                dataType: "json",
                success: function (data) {
                    switch (data.status) {
                        case 2:
                            alert("删除成功！");
                            window.location.reload();
                            break;
                        case 4: break;
                        default:
                            alert("出错啦，联系管理员！");
                            break;
                    }
                }
            });
        }
    });
    function cleanForm() {
        if ($("input#alias").attr("readonly") != true) $("input#alias").val("");
        $("input#iptTeam").val("");
        $("input#iptPosition").val("");
        $("input#email").val("");
        $("input#iptTeam").hide();
        $("input#iptPosition").hide();
        $("span", $("select#selTeam").parent()).text(" *");
        $("span", $("select#selPosition").parent()).text(" *");
        $("span", $("#email").parent()).text(" *");
    }
    //提交资产表单
    $("input#btnSubmit_Asset").click(function () {
        if ($("select#selAssetID option:selected").val() == "0") {
            $("span", $("select#selAssetID").parent()).text("没有选择正确的Asset！");
            return false;
        }
        var owner = $("input#hideNewUserAlias").val();
        $.ajax({
            url: "../../asset/updateOwner",
            type: "POST",
            data: { "assetID": $("select#selAssetID option:selected").val(), "owner": owner },
            dataType: "json",
            success: function (data) {
                if (data.status == 2) {
                    alert("资产信息添加成功！");
                    loadAssetsInfo(owner);
                } else {
                    alert("出错啦，联系管理员！");
                }
            }
        });
    });
    //提交贝尔卡表单
    $("input#btnSubmit_cardInBell").click(function () {
        if ($("input#accountID").val() == "") {
            $("span", $("input#accountID").parent()).text("Bell账号不能为空！");
            return false;
        }
        if ($("input#employeeID").val() == "") {
            $("span", $("input#employeeID").parent()).text("Bell工号不能为空！");
            return false;
        }
        var owner = $("input#hideNewUserAlias").val();
        $.ajax({
            url: "../../BellAccount/Create",
            type: "POST",
            data: { "accountID": $("input#accountID").val(), "empNo": $("input#employeeID").val(), "status": $("select#accountStatus option:selected").val(), "domain": $("input#accountDomain").val(), "belongto": owner, "latestUpdateDate": $("input#latestModify").val() },
            dataType: "json",
            success: function (data) {
                switch (data.status) {
                    case 1:
                        $("span", $("input#accountID").parent()).text("Bell账号已经存在！");
                        break;
                    case 2:
                        alert("bell账号信息添加成功！");
                        loadAcc_cardInfo(owner, "hiddenCard");
                        break;
                    case 4:
                    default:
                        alert("出错啦，联系管理员！");
                        break;
                }
            }
        });
    });
    //员工卡表单
    $("input#btnSubmit_Card").click(function () {
        if ($("input#cardNo").val() == "") {
            $("span", $("input#cardNo").parent()).text("bell工号不能为空！");
            return false;
        }
        var owner = $("input#hideNewUserAlias").val();
        $.ajax({
            url: "../../card/submit",
            type: "POST",
            data: { "cardNo": $("input#cardNo").val(), "status": $("select#cardstatus option:selected").val(), "belongto": owner, "latestUpdateDate": $("#cardLatestUpdateDate").val() },
            dataType: "json",
            success: function (data) {
                switch (data.status) {
                    case 1:
                        $("span", $("input#cardNo").parent()).text("bell工号已经存在！");
                        break;
                    case 2:
                        $("div.popupWindow").hide();
                        $("#greybackground").hide();
                        alert("操作成功！");
                        window.location.reload();
                        break;
                    case 4:
                    default:
                        alert("出错啦，联系管理员！");
                        break;
                }
            }
        });
    });
    function loadAcc_cardInfo(userAlias, type) {
        $("div.popupWindow").empty();
        $("div.popupWindow").append($("div." + type).children());
        $("div.popupWindow").append(("<input type='hidden' id='hideNewUserAlias' value='" + userAlias + "' />"));
        if (type == "hiddenAccount") {
            $('#latestModify').datepicker();
            $("h2:eq(0)", $("div.popupWindow")).html("为用户<b>" + userAlias + "</b>添加bell账号");
        } else {
            $('#cardlatestUpdateDate').datepicker();
            $("h2:eq(0)", $("div.popupWindow")).html("为用户<b>" + userAlias + "</b>添加bell磁卡");
        }
    }
    //跳过信息
    $("input.btnSkip").click(function () {
        var alias = $("input#hideNewUserAlias").val();
        switch (this.name) {
            case "asset":
                loadAcc_cardInfo(alias, "hiddenAccount");
                break;
            case "account":
                loadAcc_cardInfo(alias, "hiddenCard");
                break;
            case "card":
                $("div.popupWindow").hide();
                $("#greybackground").hide();
                alert("操作成功！");
                window.location.reload();
                break;
            default:
                break;
        }
    });
})